﻿using BCCommon;
using BCCommon.PaymentEnums;
using BCDto.Common.ErrorMessage;
using BCDto.Common.FlowRecord;
using BCDto.Common.Wallet;
using BCService.Common.Wallet;
using BCService.SmsService;
using Microsoft.AspNetCore.Mvc;
using System;


namespace BCTMSRESTful.Controllers.DriverApp.Wallet
{
    /// <summary>
    /// 钱包接口
    /// </summary>
    [ApiExplorerSettings(GroupName = "DriverApp")]
    [Route("api/v1/DriverApp/[controller]")]
    public class WalletController : DriverAppBaseController
    {
        private readonly IWalletService walletService;
        private readonly IAliyunSmsService aliyunSmsService;
        public WalletController(IWalletService walletService, IAliyunSmsService aliyunSmsService)
        {
            this.walletService = walletService;
            this.aliyunSmsService = aliyunSmsService;
        }

        #region 获取钱包信息
        /// <summary>
        /// 获取钱包信息
        /// </summary>
        /// <response code="200">返回钱包信息</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(WalletDto), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult Get()
        {
            return Ok(this.walletService.GetWalletByUserId(this.UserInfo.UserId));
        }
        #endregion

        #region 首次设置支付密码
        /// <summary>
        /// 首次设置支付密码
        /// </summary>
        /// <response code="200">返回钱包信息</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPost("[action]")]
        [ProducesResponseType(typeof(WalletDto), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult FirstSetPayPassword([FromBody]WalletRequestDto walletRequestDto)
        {
            return Ok(this.walletService.FirstSetPayPassword(this.UserInfo, walletRequestDto));
        }
        #endregion

        #region 重置设置支付密码
        /// <summary>
        /// 重置设置支付密码
        /// </summary>
        /// <response code="200">返回钱包信息</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPost("[action]")]
        [ProducesResponseType(typeof(WalletDto), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult ResetPayPassword([FromBody]WalletResetRequestDto walletRequestDto)
        {
            if (walletRequestDto.OperatingType == ResetOperatingType.Reset)
            {
                if (!this.aliyunSmsService.ValidatePassCode(this.UserInfo.Phone, walletRequestDto.PassCode))
                {
                    throw new ArgumentException("验证码验证失败");
                }
            }
            if (walletRequestDto.OperatingType == ResetOperatingType.Modify)
            {
                if (string.IsNullOrWhiteSpace(walletRequestDto.SourcePassword))
                {
                    throw new ArgumentException("原始支付密码不能为空");
                }
            }
            if (string.IsNullOrWhiteSpace(walletRequestDto.PayPassword))
            {
                throw new ArgumentException("支付密码不能为空");
            }

            return Ok(this.walletService.ResetPayPassword(this.UserInfo.Phone,this.UserInfo.UserId, walletRequestDto));
        }
        #endregion


        #region 查看指定用户的账单明细

        /// <summary>
        /// 查看指定用户的账单明细
        /// </summary>
        /// <param name="ascending">排序</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页数据条数</param>
        /// <response code="200">指定用户的账单明细列表</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<UserFlowRecordDto>), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult UserFlowRecord(
            [FromQuery(Name = "ascending")]bool? ascending = null,
            [FromQuery(Name = "index")]int? pageIndex = 1,
            [FromQuery(Name = "size")]int? pageSize = 10)
        {
            if (this.User == null)
            {

                throw new ArgumentException("登录超时，请重新登录");


            }
            return this.Ok(this.walletService.GetFlowDetails(this.UserInfo.UserId, ascending, pageIndex.Value, pageSize.Value));
        }

        #endregion


    }
}
